import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus')
    },
    language: Cookies.get('language') || 'zh',
    fullHeight: document.documentElement.clientHeight,
    pageList: Cookies.get('pageList') || [5, 10, 15, 20, 30, 40, 50]
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
    },
    SET_LANGUAGE: (state, language) => {
      state.language = language
      Cookies.set('language', language)
    },
    SET_FULLHEIGHT: state => {
      state.fullHeight = document.documentElement.clientHeight
    },
    SET_PAGELIST: (state, pageList) => {
      state.pageList = pageList
      Cookies.set('pageList', pageList)
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    setLanguage({ commit }, language) {
      commit('SET_LANGUAGE', language)
    },
    setFullHeight: ({ commit }) => {
      window.addEventListener('resize', commit('SET_FULLHEIGHT'))
    },
    setPageList: ({ commit, pageList }) => {
      commit('SET_PAGELIST', pageList)
    }
  }
}

export default app
